home *** CD-ROM | disk | FTP | other *** search
/ IRIX Base Documentation 2002 November / SGI IRIX Base Documentation 2002 November.iso / usr / share / catman / p_man / cat3 / perl5 / Benchmark.z / Benchmark
Encoding:
Text File  |  2002-10-03  |  10.2 KB  |  331 lines

  1.  
  2.  
  3.  
  4. BBBBeeeennnncccchhhhmmmmaaaarrrrkkkk((((3333))))                                                      BBBBeeeennnncccchhhhmmmmaaaarrrrkkkk((((3333))))
  5.  
  6.  
  7.  
  8. NNNNAAAAMMMMEEEE
  9.      Benchmark - benchmark running times of code
  10.  
  11.      timethis - run a chunk of code several times
  12.  
  13.      timethese - run several chunks of code several times
  14.  
  15.      timeit - run a chunk of code and see how long it goes
  16.  
  17. SSSSYYYYNNNNOOOOPPPPSSSSIIIISSSS
  18.          timethis ($count, "code");
  19.  
  20.          # Use Perl code in strings...
  21.          timethese($count, {
  22.              'Name1' => '...code1...',
  23.              'Name2' => '...code2...',
  24.          });
  25.  
  26.          # ... or use subroutine references.
  27.          timethese($count, {
  28.              'Name1' => sub { ...code1... },
  29.              'Name2' => sub { ...code2... },
  30.          });
  31.  
  32.          $t = timeit($count, '...other code...')
  33.          print "$count loops of other code took:",timestr($t),"\n";
  34.  
  35.  
  36. DDDDEEEESSSSCCCCRRRRIIIIPPPPTTTTIIIIOOOONNNN
  37.      The Benchmark module encapsulates a number of routines to help you figure
  38.      out how long it takes to execute some code.
  39.  
  40.      MMMMeeeetttthhhhooooddddssss
  41.  
  42.      new       Returns the current time.   Example:
  43.  
  44.                    use Benchmark;
  45.                    $t0 = new Benchmark;
  46.                    # ... your code here ...
  47.                    $t1 = new Benchmark;
  48.                    $td = timediff($t1, $t0);
  49.                    print "the code took:",timestr($td),"\n";
  50.  
  51.  
  52.      debug     Enables or disable debugging by setting the $Benchmark::Debug
  53.                flag:
  54.  
  55.                    debug Benchmark 1;
  56.                    $t = timeit(10, ' 5 ** $Global ');
  57.                    debug Benchmark 0;
  58.  
  59.  
  60.  
  61.  
  62.  
  63.                                                                         PPPPaaaaggggeeee 1111
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70. BBBBeeeennnncccchhhhmmmmaaaarrrrkkkk((((3333))))                                                      BBBBeeeennnncccchhhhmmmmaaaarrrrkkkk((((3333))))
  71.  
  72.  
  73.  
  74.      SSSSttttaaaannnnddddaaaarrrrdddd EEEExxxxppppoooorrrrttttssss
  75.  
  76.      The following routines will be exported into your namespace if you use
  77.      the Benchmark module:
  78.  
  79.      timeit(COUNT, CODE)
  80.                Arguments: COUNT is the number of times to run the loop, and
  81.                CODE is the code to run.  CODE may be either a code reference
  82.                or a string to be eval'd; either way it will be run in the
  83.                caller's package.
  84.  
  85.                Returns: a Benchmark object.
  86.  
  87.      timethis ( COUNT, CODE, [ TITLE, [ STYLE ]] )
  88.                Time COUNT iterations of CODE. CODE may be a string to eval or
  89.                a code reference; either way the CODE will run in the caller's
  90.                package.  Results will be printed to STDOUT as TITLE followed
  91.                by the times.  TITLE defaults to "timethis COUNT" if none is
  92.                provided. STYLE determines the format of the output, as
  93.                described for _t_i_m_e_s_t_r() below.
  94.  
  95.                The COUNT can be zero or negative: this means the _m_i_n_i_m_u_m
  96.                _n_u_m_b_e_r _o_f _C_P_U _s_e_c_o_n_d_s to run.  A zero signifies the default of
  97.                3 seconds.  For example to run at least for 10 seconds:
  98.  
  99.                        timethis(-10, $code)
  100.  
  101.                or to run two pieces of code tests for at least 3 seconds:
  102.  
  103.                        timethese(0, { test1 => '...', test2 => '...'})
  104.  
  105.                CPU seconds is, in UNIX terms, the user time plus the system
  106.                time of the process itself, as opposed to the real (wallclock)
  107.                time and the time spent by the child processes.  Less than 0.1
  108.                seconds is not accepted (-0.01 as the count, for example, will
  109.                cause a fatal runtime exception).
  110.  
  111.                Note that the CPU seconds is the mmmmiiiinnnniiiimmmmuuuummmm time: CPU scheduling
  112.                and other operating system factors may complicate the attempt
  113.                so that a little bit more time is spent.  The benchmark output
  114.                will, however, also tell the number of $code runs/second, which
  115.                should be a more interesting number than the actually spent
  116.                seconds.
  117.  
  118.                Returns a Benchmark object.
  119.  
  120.      timethese ( COUNT, CODEHASHREF, [ STYLE ] )
  121.                The CODEHASHREF is a reference to a hash containing names as
  122.                keys and either a string to eval or a code reference for each
  123.                value.  For each (KEY, VALUE) pair in the CODEHASHREF, this
  124.                routine will call
  125.  
  126.  
  127.  
  128.  
  129.                                                                         PPPPaaaaggggeeee 2222
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136. BBBBeeeennnncccchhhhmmmmaaaarrrrkkkk((((3333))))                                                      BBBBeeeennnncccchhhhmmmmaaaarrrrkkkk((((3333))))
  137.  
  138.  
  139.  
  140.                        timethis(COUNT, VALUE, KEY, STYLE)
  141.  
  142.                The routines are called in string comparison order of KEY.
  143.  
  144.                The COUNT can be zero or negative, see _t_i_m_e_t_h_i_s().
  145.  
  146.      timediff ( T1, T2 )
  147.                Returns the difference between two Benchmark times as a
  148.                Benchmark object suitable for passing to _t_i_m_e_s_t_r().
  149.  
  150.      timestr ( TIMEDIFF, [ STYLE, [ FORMAT ] ] )
  151.                Returns a string that formats the times in the TIMEDIFF object
  152.                in the requested STYLE. TIMEDIFF is expected to be a Benchmark
  153.                object similar to that returned by _t_i_m_e_d_i_f_f().
  154.  
  155.                STYLE can be any of 'all', 'noc', 'nop' or 'auto'. 'all' shows
  156.                each of the 5 times available ('wallclock' time, user time,
  157.                system time, user time of children, and system time of
  158.                children). 'noc' shows all except the two children times. 'nop'
  159.                shows only wallclock and the two children times. 'auto' (the
  160.                default) will act as 'all' unless the children times are both
  161.                zero, in which case it acts as 'noc'.
  162.  
  163.                FORMAT is the the _p_r_i_n_t_f(_3) manpage-style format specifier
  164.                (without the leading '%') to use to print the times. It
  165.                defaults to '5.2f'.
  166.  
  167.      OOOOppppttttiiiioooonnnnaaaallll EEEExxxxppppoooorrrrttttssss
  168.  
  169.      The following routines will be exported into your namespace if you
  170.      specifically ask that they be imported:
  171.  
  172.      clearcache ( COUNT )
  173.                Clear the cached time for COUNT rounds of the null loop.
  174.  
  175.      clearallcache ( )
  176.                Clear all cached times.
  177.  
  178.      disablecache ( )
  179.                Disable caching of timings for the null loop. This will force
  180.                Benchmark to recalculate these timings for each new piece of
  181.                code timed.
  182.  
  183.      enablecache ( )
  184.                Enable caching of timings for the null loop. The time taken for
  185.                COUNT rounds of the null loop will be calculated only once for
  186.                each different COUNT used.
  187.  
  188. NNNNOOOOTTTTEEEESSSS
  189.      The data is stored as a list of values from the time and times functions:
  190.  
  191.  
  192.  
  193.  
  194.  
  195.                                                                         PPPPaaaaggggeeee 3333
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202. BBBBeeeennnncccchhhhmmmmaaaarrrrkkkk((((3333))))                                                      BBBBeeeennnncccchhhhmmmmaaaarrrrkkkk((((3333))))
  203.  
  204.  
  205.  
  206.            ($real, $user, $system, $children_user, $children_system)
  207.  
  208.      in seconds for the whole loop (not divided by the number of rounds).
  209.  
  210.      The timing is done using _t_i_m_e(3) and _t_i_m_e_s(3).
  211.  
  212.      Code is executed in the caller's package.
  213.  
  214.      The time of the null loop (a loop with the same number of rounds but
  215.      empty loop body) is subtracted from the time of the real loop.
  216.  
  217.      The null loop times are cached, the key being the number of rounds. The
  218.      caching can be controlled using calls like these:
  219.  
  220.          clearcache($key);
  221.          clearallcache();
  222.  
  223.          disablecache();
  224.          enablecache();
  225.  
  226.  
  227. IIIINNNNHHHHEEEERRRRIIIITTTTAAAANNNNCCCCEEEE
  228.      Benchmark inherits from no other class, except of course for Exporter.
  229.  
  230. CCCCAAAAVVVVEEEEAAAATTTTSSSS
  231.      Comparing eval'd strings with code references will give you inaccurate
  232.      results: a code reference will show a slower execution time than the
  233.      equivalent eval'd string.
  234.  
  235.      The real time timing is done using _t_i_m_e(2) and the granularity is
  236.      therefore only one second.
  237.  
  238.      Short tests may produce negative figures because perl can appear to take
  239.      longer to execute the empty loop than a short test; try:
  240.  
  241.          timethis(100,'1');
  242.  
  243.      The system time of the null loop might be slightly more than the system
  244.      time of the loop with the actual code and therefore the difference might
  245.      end up being < 0.
  246.  
  247. AAAAUUUUTTTTHHHHOOOORRRRSSSS
  248.      Jarkko Hietaniemi <_j_h_i@_i_k_i._f_i>, Tim Bunce <_T_i_m._B_u_n_c_e@_i_g._c_o._u_k>
  249.  
  250. MMMMOOOODDDDIIIIFFFFIIIICCCCAAAATTTTIIIIOOOONNNN HHHHIIIISSSSTTTTOOOORRRRYYYY
  251.      September 8th, 1994; by Tim Bunce.
  252.  
  253.      March 28th, 1997; by Hugo van der Sanden: added support for code
  254.      references and the already documented 'debug' method; revamped
  255.      documentation.
  256.  
  257.  
  258.  
  259.  
  260.  
  261.                                                                         PPPPaaaaggggeeee 4444
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268. BBBBeeeennnncccchhhhmmmmaaaarrrrkkkk((((3333))))                                                      BBBBeeeennnncccchhhhmmmmaaaarrrrkkkk((((3333))))
  269.  
  270.  
  271.  
  272.      April 04-07th, 1997: by Jarkko Hietaniemi, added the run-for-some-time
  273.      functionality.
  274.  
  275.  
  276.  
  277.  
  278.  
  279.  
  280.  
  281.  
  282.  
  283.  
  284.  
  285.  
  286.  
  287.  
  288.  
  289.  
  290.  
  291.  
  292.  
  293.  
  294.  
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.                                                                         PPPPaaaaggggeeee 5555
  328.  
  329.  
  330.  
  331.